<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
$bool=verifLibelle1('Code_region') && verifLibelle('Nom_region') &&   verifLibelle('Identifiant_utilisateur') && verifLibelle('Identifiant_direction') ;

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $bool && !existeChamp($database_microassur, $microassur,'region','Code_region',@$_POST['Code_region'])) {
  $insertSQL = sprintf("INSERT INTO region (Identifiant_region, Code_region, Nom_region, Identifiant_utilisateur, Identifiant_direction) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_region'], "int"),
                       GetSQLValueString($_POST['Code_region'], "text"),
                       GetSQLValueString($_POST['Nom_region'], "text"),
                       GetSQLValueString($_POST['Identifiant_utilisateur'], "int"),
                       GetSQLValueString($_POST['Identifiant_direction'], "int"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  if($Result1){mysql_query("commit");header("location:index.php?ajout=success");}
else {mysql_query("rollback");$message=colorer("Contacter la DSI, erreur création région","false");}

}
elseif(isset($_POST["MM_insert"])&& !$bool) {

if(empty($_POST['Code_region'])) $message=colorer("Vous devez saisir le code","false");
elseif(!verifLibelle1('Code_region')) $message=colorer("Le code saisi comporte des caractères spéciaux interdits","false");

elseif(empty($_POST['Nom_region'])) $message=colorer("Vous devez saisir le nom ","false");
elseif(!verifLibelle('Nom_region')) $message=colorer("Le nom saisi comporte des caractères spéciaux interdits ","false");

else
//$message=colorer("Erreur: Au moins un champ est vide ou valeur incorrecte","false");
$message="";


}


elseif(isset($_POST["MM_insert"]) && existeChamp($database_microassur, $microassur,'region','Code_region',@$_POST['Code_region']))$message=colorer("Cette région existe déjà","false") ;

/* Liste des utilisateurs directeur de région */ /* Modif Thia */
mysql_select_db($database_microassur, $microassur);
$query_rs_r = 'SELECT * FROM fonction WHERE upper(Libelle_fonction) LIKE "%GION%" ';
$rs_r = mysql_query($query_rs_r, $microassur) or die(mysql_error());
$row_rs_r = mysql_fetch_assoc($rs_r);
if(empty($row_rs_r) || !$row_rs_r) $row_rs_r['Identifiant_fonction'] = 0;
$totalRows_rs_r = mysql_num_rows($rs_r);

mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT * FROM region";
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);

mysql_select_db($database_microassur, $microassur);
$query_rs_dir = "SELECT * FROM direction";
$rs_dir = mysql_query($query_rs_dir, $microassur) or die(mysql_error());
$row_rs_dir = mysql_fetch_assoc($rs_dir);
$totalRows_rs_dir = mysql_num_rows($rs_dir);

mysql_select_db($database_microassur, $microassur);
$query_rs_usr = 'SELECT * FROM utilisateur WHERE Identifiant_fonction ='.$row_rs_r['Identifiant_fonction']; /* modif Thia */
$rs_usr = mysql_query($query_rs_usr, $microassur) or die(mysql_error());
$row_rs_usr = mysql_fetch_assoc($rs_usr);
$totalRows_rs_usr = mysql_num_rows($rs_usr);
?>
<?php  if(isset($_GET['ajout']) && $_GET['ajout']='success') $message=colorer("La région est créée","true");?>

<?php if(isset($_GET['supp']) && $_GET['supp']=='err') $message=colorer("Contacter la DSI, erreur suppression région","false");?>
<?php if(isset($_GET['mod']) && $_GET['mod']=='success') $message=colorer("La modification de la région est prise en compte","true");?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Région";
					$nommodule2="Liste des régions";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
                <table class="tab_list bordh">
                  <tr id="niv1">
                    <td class="col1">CODE</td>							
                    <td class="col2">NOM</td>
                    <td>NOM DIRIGEANT</td>
                    <td class="col2">DIRECTION</td>
                  </tr>
                </table>
				<div class="content_table">
					<table class="tab_list">
						<?php $i=1; do 	{ 
						  $Identifiant_utilisateur_rs_user_liste = "-1";
						if (isset($row_rs['Identifiant_utilisateur'])) {
						  $Identifiant_utilisateur_rs_user_liste = $row_rs['Identifiant_utilisateur'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs_user_liste = sprintf("SELECT * FROM utilisateur WHERE Identifiant_utilisateur = %s", GetSQLValueString($Identifiant_utilisateur_rs_user_liste, "text"));
						$rs_user_liste = mysql_query($query_rs_user_liste, $microassur) or die(mysql_error());
						$row_rs_user_liste = mysql_fetch_assoc($rs_user_liste);
						$totalRows_rs_user_liste = mysql_num_rows($rs_user_liste);

						$Identifiant_direction_rs_dir_liste = "-1";
						if (isset($row_rs['Identifiant_direction'])) {
						  $Identifiant_direction_rs_dir_liste = $row_rs['Identifiant_direction'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs_dir_liste = sprintf("SELECT * FROM direction WHERE Identifiant_direction = %s", GetSQLValueString($Identifiant_direction_rs_dir_liste, "text"));
						$rs_dir_liste = mysql_query($query_rs_dir_liste, $microassur) or die(mysql_error());
						$row_rs_dir_liste = mysql_fetch_assoc($rs_dir_liste);
						$totalRows_rs_dir_liste = mysql_num_rows($rs_dir_liste);
						   ?>    <?php if ($totalRows_rs > 0) { // Show if recordset not empty ?>

							<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
							  <td class="col1"><a href="modregion.php?Identifiant_region=<?php echo $row_rs['Identifiant_region']; ?>"><?php echo $row_rs['Code_region']; ?></a></td>
							  <td class="col2"><a href="modregion.php?Identifiant_region=<?php echo $row_rs['Identifiant_region']; ?>"><?php echo tronque($row_rs['Nom_region'],20); ?></a></td>
							  <td><a href="modregion.php?Identifiant_region=<?php echo $row_rs['Identifiant_region']; ?>"><?php echo $row_rs_user_liste['Prenom_utilisateur']; ?> <?php echo tronque($row_rs_user_liste['Nom_utilisateur'],20); ?></a></td>
							  <td class="col2"><a href="modregion.php?Identifiant_region=<?php echo $row_rs['Identifiant_region']; ?>"><?php echo tronque($row_rs_dir_liste['Nom_direction'],22); ?></a></td>
							</tr>
							  <?php } // Show if recordset not empty ?>
									<?php $i++; } while ($row_rs = mysql_fetch_assoc($rs)); ?>
						</table>
						</div>
					</div>
					<div class="imgright"></div>
				<div class="cleared"></div>
				<!--/***************************************/-->
				<div id="br"></div>
				
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
					<div class="formulaire">
							<table class="tabform">
								<tr>
								  <td>CODE</td>
								  <td><input class="resize_1" type="text" maxlength="5" name="Code_region" value="<?php if(isset($_POST['Code_region']))echo remind_text('Code_region');?>" size="32" /></td>
								</tr>
								<tr>
								  <td>
								  NOM</td>
								  <td><input name="Nom_region"  maxlength="30" type="text" value="<?php if(isset($_POST['Nom_region']))echo remind_text('Nom_region');?>" size="40" /></td>
								</tr>
								<tr>
								  <td>NOM DIRIGEANT</td>
								  <td><select name="Identifiant_utilisateur">
									<?php
							do {  
							?>
									<option value="<?php echo $row_rs_usr['Identifiant_utilisateur']?>"><?php echo $row_rs_usr['Prenom_utilisateur']." ".$row_rs_usr['Nom_utilisateur']?></option>
									<?php
							} while ($row_rs_usr = mysql_fetch_assoc($rs_usr));
							  $rows = mysql_num_rows($rs_usr);
							  if($rows > 0) {
								  mysql_data_seek($rs_usr, 0);
								  $row_rs_usr = mysql_fetch_assoc($rs_usr);
							  }
							?>
								  </select>
								  </td>
								</tr>
								<tr>
								  <td>DIRECTION</td>
								  <td><select name="Identifiant_direction">
									  <?php
							do {  
							?>
									  <option value="<?php echo $row_rs_dir['Identifiant_direction']?>"><?php echo $row_rs_dir['Nom_direction']?></option>
									  <?php
							} while ($row_rs_dir = mysql_fetch_assoc($rs_dir));
							  $rows = mysql_num_rows($rs_dir);
							  if($rows > 0) {
								  mysql_data_seek($rs_dir, 0);
								  $row_rs_dir = mysql_fetch_assoc($rs_dir);
							  }
							?>
								  </select>
								  </td>
								</tr>
							  </table>
					</div>
			</div>
			<div class="imgright"></div>				
				<div class="cleared"></div>
				
					<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
					<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
					
				<div class="cleared"></div>
  <input type="hidden" name="Identifiant_region" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);
mysql_free_result($rs_dir);
mysql_free_result($rs_usr);
mysql_free_result($rs_user_liste);
mysql_free_result($rs_dir_liste);
?>